﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Data.SqlClient;

namespace HotelManagement
{
    public partial class ReserveFrm : Form
    {
        SqlConnection Cnn = new SqlConnection(Globals.DBConnection());
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        SqlDataReader dr;

        public bool FromCustomerFRM;
        public int c_id = -1;
        public string cname;
        public string lname;
        public int rm_id;
        public string RoomPrice;
        public DateTime sdate;
        public DateTime edate;

        public ReserveFrm()
        {
            InitializeComponent();
        }

        public void setfnametxt(string txt)
        {
            fnameTxt.Text = txt;
        }

        public string lnametxt
        {
            get { return lnameTxt.Text; }
            set { lnameTxt.Text = value; }
        }

        public void ReserveFrm_Load(object sender, EventArgs e)
        {
            toolStripUsername.Text = "نام کاربر : " + Globals.GetUserName();

            PersianCalendar PC = new PersianCalendar();
            string shamsidate =
                PC.GetYear(DateTime.Now).ToString() + "/" +
                PC.GetMonth(DateTime.Now).ToString() + "/" +
                PC.GetDayOfMonth(DateTime.Now).ToString();

            toolStripDate.Text = "تاریخ : " + shamsidate;

            toolStripTime.Text = DateTime.Now.ToLongTimeString();

            if (FromCustomerFRM)
            {
                SqlCommand CustomerDetail = new SqlCommand("SELECT c_id,c_fname,c_lname FROM Customer WHERE c_id = (SELECT MAX(c_id) FROM Customer)", Cnn);
                Cnn.Open();
                dr = CustomerDetail.ExecuteReader();
                dr.Read();
                fnameTxt.Text = dr["c_fname"].ToString();
                lnameTxt.Text = dr["c_lname"].ToString();
                c_id = (int)dr["c_id"];
                Cnn.Close();
            }

            da = new SqlDataAdapter("select rs_id as [شماره نامه رزرو],c_fname as[نام],c_lname as[نام خانوادگی],rs_sdate as[تاریخ شروع],rs_edate as[تاریخ پایان],rs_persons as[تعداد همراهان],rm_number as[شماره اتاق],rs_tprice as[هزینه] from Reserve,Customer,Room Where rm_id=rs_rm_id AND c_id=rs_c_id order by rs_id desc", Cnn);
            da.SelectCommand.Parameters.AddWithValue("@cid", c_id);

            da.Fill(ds);
            dGView_Reserve.DataSource = ds.Tables[0];

        }

        private void button2_Click(object sender, EventArgs e)
        {
            DayPicker form = new DayPicker();
            form.ShowDialog();

            PersianCalendar PC = new PersianCalendar();
            sdate = new DateTime(form.getyear(), form.getmonth(), form.getday());
            string shamsidate =
                PC.GetYear(sdate).ToString() + "/" +
                PC.GetMonth(sdate).ToString() + "/" +
                PC.GetDayOfMonth(sdate).ToString();

            textBox7.Text = shamsidate;
        }

        private void CustomerSearchBTN_Click(object sender, EventArgs e)
        {
            CustomerSearchFRM csf = new CustomerSearchFRM();
            csf.ShowDialog();
            if (csf.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                fnameTxt.Text = csf.c_fname;
                lnameTxt.Text = csf.c_lname;
                c_id = csf.c_id;
            }
        }

        private void RoomSearchBTN_Click(object sender, EventArgs e)
        {
            RoomSearchFRM rsf = new RoomSearchFRM();
            rsf.ShowDialog();
            if (rsf.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                rmnumberTxt.Text = rsf.RoomNumber;
                rm_id = rsf.RoomID;
                RoomPrice = rsf.RoomPrice;
            }


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripTime.Text = DateTime.Now.ToLongTimeString();
        }

        private void EndDateBtn_Click(object sender, EventArgs e)
        {
            DayPicker form = new DayPicker();
            form.ShowDialog();

            PersianCalendar PC = new PersianCalendar();
            edate = new DateTime(form.getyear(), form.getmonth(), form.getday());
            string shamsidate =
                PC.GetYear(edate).ToString() + "/" +
                PC.GetMonth(edate).ToString() + "/" +
                PC.GetDayOfMonth(edate).ToString();

            EndDateTxt.Text = shamsidate;
        }

        private void NumberOfPersonsBtn_Click(object sender, EventArgs e)
        {
            if (c_id != -1)
            {
                ComradeCreate cc = new ComradeCreate();
                cc.c_id = c_id;
                cc.ShowDialog();

                SqlCommand Cmd = new SqlCommand("select COUNT(pr_id) from Person where pr_c_id = @cid", Cnn);
                Cmd.Parameters.AddWithValue("@cid", c_id);

                Cnn.Open();
                dr = Cmd.ExecuteReader();
                dr.Read();
                PersonsTxt.Text = dr[0].ToString();
                Cnn.Close();
            }

            else
            {
                MessageBox.Show("ابتدا مشخصات مهمان را وارد کنید");
            }
        }

        private void ReserveSaveBtn_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(fnameTxt.Text) && !string.IsNullOrEmpty(lnameTxt.Text) && !string.IsNullOrEmpty(textBox7.Text) && !string.IsNullOrEmpty(EndDateTxt.Text) && !string.IsNullOrEmpty(PersonsTxt.Text) && !string.IsNullOrEmpty(rmnumberTxt.Text))
            {

                if (CheckRoomAndData(rm_id, sdate, edate))
                {
                    SqlCommand SaveINDB = new SqlCommand("Insert into Reserve (rs_c_id,rs_rm_id,rs_u_id,rs_sdate,rs_edate,rs_persons,rs_submitdate,rs_submittime,rs_tprice) values (@c_id,@rm_id,@u_id,@sdate,@edate,@persons,@submitdate,@submittime,@tprice)", Cnn);
                    SaveINDB.Parameters.AddWithValue("@c_id", c_id);
                    SaveINDB.Parameters.AddWithValue("@rm_id", rm_id);
                    SaveINDB.Parameters.AddWithValue("@u_id", Globals.GetUserID());
                    SaveINDB.Parameters.AddWithValue("@sdate", sdate);
                    SaveINDB.Parameters.AddWithValue("@edate", edate);
                    SaveINDB.Parameters.AddWithValue("@persons", int.Parse(PersonsTxt.Text));
                    SaveINDB.Parameters.AddWithValue("@submitdate", DateTime.Now.ToShortDateString());
                    SaveINDB.Parameters.AddWithValue("@submittime", DateTime.Now.ToShortTimeString());

                    string RoomPriceStr = ((Int64)((edate - sdate).TotalDays * Int64.Parse(RoomPrice))).ToString();
                    SaveINDB.Parameters.AddWithValue("@tprice", RoomPrice);

                    Cnn.Open();
                    SaveINDB.ExecuteNonQuery();
                    Cnn.Close();


                    // find the last id for save in factor table
                    SqlCommand lastReservId = new SqlCommand("SELECT MAX(rs_id) FROM Reserve", Cnn);
                    SqlDataReader DR;
                    Cnn.Open();
                    DR = lastReservId.ExecuteReader();
                    DR.Read();
                    int rs_id = (int)DR[0];
                    Cnn.Close();


                    SqlCommand FactorSaveINDB = new SqlCommand("insert into Factor (f_rs_id,f_tprice) values (@rs_id,@tprice)", Cnn);
                    FactorSaveINDB.Parameters.AddWithValue("@rs_id", rs_id);
                    FactorSaveINDB.Parameters.AddWithValue("@tprice", RoomPriceStr);

                    Cnn.Open();
                    FactorSaveINDB.ExecuteNonQuery();
                    Cnn.Close();



                    ds.Clear();
                    da.Fill(ds);
                    dGView_Reserve.DataSource = ds.Tables[0];
                }
            }

            else
            {
                MessageBox.Show("لطفا همه قسمت ها را به درستی تکمیل نمایید");
            }
        }


        public Boolean CheckRoomAndData(int rm_id, DateTime sdate, DateTime edate)
        {

            if ( sdate < DateTime.Today)
            {
                MessageBox.Show("زمان رزرو گذشته است");
                return false;
            }

            if ((int)(edate - sdate).TotalDays < 0)
            {
                MessageBox.Show("لطفا تاریخ های شروع و پایان را بررسی کنید");
                return false;
            }

            SqlCommand ChekDays = new SqlCommand("select rs_id,rs_sdate,rs_edate from Reserve,Room WHERE ((rs_sdate between @sdate AND @edate) or (rs_edate between @sdate AND @edate) or (@sdate between rs_sdate AND rs_edate) or (@edate between rs_sdate AND rs_edate)) AND rs_rm_id=rm_id AND rm_id = @rm_id", Cnn);
            ChekDays.Parameters.AddWithValue("@rm_id", rm_id);
            ChekDays.Parameters.AddWithValue("@sdate", sdate);
            ChekDays.Parameters.AddWithValue("@edate", edate);
            SqlDataReader DR;
            Cnn.Open();
            DR = ChekDays.ExecuteReader();
            if (DR.HasRows)
            {
                MessageBox.Show(".اتاق انتخاب شده قبلا رزرو شده است");
                Cnn.Close();
                return false;
            }
            Cnn.Close();

            return true;
        }

    }
}